package net.xju_3_4.mapper;

import net.xju_3_4.entity.Student;
import net.xju_3_4.entity.StudentCard;
import net.xju_3_4.entity.StudentClazz;
import net.xju_3_4.entity.vo.ClazzTeachVO;
import net.xju_3_4.entity.vo.StudentVO;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@CacheNamespace  // 开启二级缓存
public interface StudentMapper {
    Integer delete(Integer id);

    @Select(" select *  from  student")
    List<Student> selectList();

    List<Student> selectById(String i);

    Student selectBySnAndName(@Param("name") String name, @Param("sn") String sn);

    Student selectMap(Map<String, Object> map);

    @MapKey("name")
    Map<String, Object> selectByName(String name);

//    @Select("  SELECT s.*,c.* FROM `card` c ,student s WHERE c.id = s.card_id AND s.id = #{id}")
//    @Results({
//            @Result(property = "id",column = "id",id = true),
//            @Result(property = "sn",column = "sn"),
//            @Result(property = "name",column = "name"),
//            @Result(property = "cardId",column = "card_id"),
//            @Result(property = "clazzId",column = "clazz_id"),
//            @Result(property = "studentCard.id",column = "id"),
//            @Result(property = "studentCard.balance",column = "balance"),
//    })

    @Select("select * from student where id =#{id}")
    @Results({
            @Result(property = "id", column = "id", id = true),
            @Result(property = "sn", column = "sn"),
            @Result(property = "name", column = "name"),
            @Result(property = "cardId", column = "card_id"),
            @Result(property = "clazzId", column = "clazz_id"),
            @Result(property = "studentCard",
                    column = "card_id",  // 关联的字段
                    javaType = StudentCard.class,
                    one = @One(select = "net.xju_3_4.mapper.StudentMapper.selectStuCardById") // 调用sql
            )
    })
    StudentVO selectStuVoById(int id);

    @Select("select * from card where id = #{id}")
    StudentCard selectStuCardById(int id);


    @Select("select * from  clazz where id = #{id}")
    @Results({
            @Result(property = "id", column = "id", id = true),
            @Result(property = "clazzName", column = "clazz_name"),
            @Result(property = "sum", column = "sum"),
            @Result(property = "studentList",
                    column = "id",
                    javaType = List.class,
                    many = @Many(select = "net.xju_3_4.mapper.StudentMapper.selectStuListByClazzId"))
    })
    StudentClazz selectClazzById(int id);

    @Select("select * from student where clazz_id = #{id}")
    List<Student> selectStuListByClazzId(Integer clazzId);

    List<ClazzTeachVO> selectClazzTeach();

    List<Student> selectListBySnAndName(Student student);

    Integer update(Student student);

    @Select({"<script>" +
            " select * from student WHERE id IN" +
            "        <foreach collection='ids' open='(' item='id' separator=',' close=')'>" +
            "            #{id}" +
            "        </foreach>" +
            "</script>"})
    List<Student> selectListByIds(@Param("ids") List<Integer> list);

    Integer batchInsert(List<Student> studentList);
}
